package com.showsu.editor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.showsu.editor.dao.TblRemoteInterfaceWarnSmsRecordMapper;
import com.showsu.editor.entity.po.TblRemoteInterfaceWarnSmsRecord;
import com.showsu.editor.enums.RowFlagEnum;
import com.showsu.editor.enums.SMSTemplateEnum;
import com.showsu.editor.manage.InterfaceAutoSwitchManager;
import com.showsu.editor.service.IRemoteInterfaceWarnSmsRecordService;
import com.showsu.editor.utils.Constant;
import com.showsu.editor.utils.DateUtil;
import com.showsu.editor.utils.sms.AliyunSMSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: William
 * @Description:
 * @Date: Created in 8:02 下午 2024/1/24
 * @Modified By:
 */
@Service
@Slf4j
public class RemoteInterfaceWarnSmsRecordServiceImpl extends ServiceImpl<TblRemoteInterfaceWarnSmsRecordMapper, TblRemoteInterfaceWarnSmsRecord> implements IRemoteInterfaceWarnSmsRecordService {

    @Autowired
    private TblRemoteInterfaceWarnSmsRecordMapper tblRemoteInterfaceWarnSmsRecordMapper;

    @Autowired
    private AliyunSMSUtil aliyunSMSUtil;

    @Autowired
    private InterfaceAutoSwitchManager interfaceAutoSwitchManager;

    @Override
    public void addRecord(Long interfaceId) {
        if (interfaceId == null) {
            return;
        }
        //这是一个非常基础的方法，它出现异常不能影响上层业务
        try {
            TblRemoteInterfaceWarnSmsRecord entity = new TblRemoteInterfaceWarnSmsRecord();
            entity.setInterfaceId(interfaceId);
            tblRemoteInterfaceWarnSmsRecordMapper.insert(entity);
        } catch (Exception e) {
            log.error("IRemoteInterfaceWarnSmsRecordService.add(Long) error", e);
        }
    }

    @Override
    public void sendWarnSmsIn15mins(Long interfaceId, String params) {
        if (interfaceId == null) {
            return;
        }
        //启动接口自动切换逻辑
        interfaceAutoSwitchManager.start(interfaceId, params);
        //阿里云短信运营商天级流控为50条
        Integer dailyCount = tblRemoteInterfaceWarnSmsRecordMapper.selectCount(new QueryWrapper<TblRemoteInterfaceWarnSmsRecord>().lambda()
                .eq(TblRemoteInterfaceWarnSmsRecord::getFlag, RowFlagEnum.UnDelete.getCode())
                .between(TblRemoteInterfaceWarnSmsRecord::getCreateTime, DateUtil.getStartTodayStr(), DateUtil.getEndTodayStr()));
        if (dailyCount.intValue() >= 50) {
            return;
        }
        //15分钟内如果有发过同一个接口的告警短信，不重复发送
        TblRemoteInterfaceWarnSmsRecord existRecordIn15Mins =
                tblRemoteInterfaceWarnSmsRecordMapper.selectOne(new QueryWrapper<TblRemoteInterfaceWarnSmsRecord>().lambda()
                .eq(TblRemoteInterfaceWarnSmsRecord::getInterfaceId, interfaceId)
                .eq(TblRemoteInterfaceWarnSmsRecord::getFlag, RowFlagEnum.UnDelete.getCode())
                .ge(TblRemoteInterfaceWarnSmsRecord::getCreateTime, DateUtil.addMinutes(new Date(), -15))
                .orderByDesc(TblRemoteInterfaceWarnSmsRecord::getInterfaceId)
                .last("limit 1"));
        if (existRecordIn15Mins != null) {
            return;
        }
        Boolean sendResult = aliyunSMSUtil.sendNotice2(Constant.ddMobile, interfaceId.toString(),
                SMSTemplateEnum.REMOTE_INTERFACE_ERROR.getAliyunTemplatCode(), SMSTemplateEnum.REMOTE_INTERFACE_ERROR.getSignature());
        if (sendResult) {
            addRecord(interfaceId);
        }
    }
}
